package com.hopedove.quanlitycheck.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.hopedove.quanlitycheck.R;
import com.hopedove.quanlitycheck.entity.XiaciInfo;

import java.util.List;


public class SJZDAdapter extends BaseAdapter {

	private Context mContext;
	private onBtnClickListener mOnBtnClickListener;
	private List<XiaciInfo> mList;
	private int currentPosition = -1;

	public SJZDAdapter(Context context, List<XiaciInfo> list) {
		super();
		mContext = context;

		// 这里集合用=号赋值，改变内部productlist 外部list一样会变
		mList = list;

	}

	@Override
	public int getCount() {

		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
		XiaciInfo xclx;
		xclx = (XiaciInfo) getItem(position);
		View view;
		final ViewHolder viewholder;
		if (convertView == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.item_xclx_layout, parent, false);

			//	view.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight));

			viewholder = new ViewHolder(view);
			view.setTag(viewholder);
		} else {
			view = convertView;
			viewholder = (ViewHolder) view.getTag();
		}


		if (TextUtils.isEmpty(xclx.getSJXMC())) {
			//viewholder.btn.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
			viewholder.btn.setVisibility(View.INVISIBLE);
			viewholder.btn.setEnabled(false);
		} else {
			viewholder.btn.setVisibility(View.VISIBLE);
			viewholder.btn.setText(xclx.getSJXMC());
		}


		if (currentPosition != position) {

			viewholder.btn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_corner_gray));
			viewholder.btn.setTextColor(mContext.getResources().getColor(R.color.black));
		}
		viewholder.btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				currentPosition = position;
				viewholder.btn.setBackground(mContext.getResources().getDrawable(R.drawable.btn_corner_red));
				viewholder.btn.setTextColor(mContext.getResources().getColor(R.color.white));
				notifyDataSetChanged();
				if (mOnBtnClickListener != null) {
					mOnBtnClickListener.onClick(position);
				}

			}
		});

		return view;

	}

	/**
	 * 设置接口
	 *
	 * @param
	 */
	public void setOnBtnClickListener(onBtnClickListener onBtnClickListener) {
		mOnBtnClickListener = onBtnClickListener;
	}

	/**
	 * 接口
	 *
	 * @author zhu_yu
	 */
	public interface onBtnClickListener {
		public void onClick(int position);
	}

	/**
	 * 对控件实例进行缓存
	 *
	 * @author zhu_yu
	 * @Description<功能详细描述>
	 * @Version [版本号, 2017-5-26]
	 */
	class ViewHolder {

		Button btn;

		public ViewHolder(View view) {

			btn = (Button) view.findViewById(R.id.btn);

		}

	}


	private int mItemHeight;

	public void setItemHeight(int itemHeight) {
		// TODO Auto-generated method stub
		mItemHeight = itemHeight;
	}

}
